﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mediatheque.Areas.Admin.Models;

namespace mediatheque.Areas.Admin.Controllers
{
    public class OeuvresController : Controller
    {
        private mediathequeEntities db = new mediathequeEntities();
        private Oeuvres myOeuvre = new Oeuvres();

        //
        // GET: /Admin/Oeuvres/

        public ActionResult Index()
        {
            var list = from oeuvres in db.oeuvres select oeuvres;

            if (list == null)
                return View("NotFound");
            else
                return View(list.ToList());
        }



        /****************************************************
         * 
         *  Gestion des films
         * 
         * 
         **/

        public ActionResult Films()
        {
            return View("Films/listeFilm", myOeuvre);
        }

        public ActionResult AddFilm()
        {
            return View("Films/addFilm");
        }

        [HttpPost]
        public ActionResult AddFilm(FormCollection data)
        {
            try
            {
                movies m = new movies
                {
                    title = data["title"],
                    length = int.Parse(data["length"]),
                    release_year = int.Parse(data["realase_year"]),
                    resume = data["resume"]
                };

                if (TryUpdateModel(m))
                {
                    db.movies.AddObject(m);
                    db.SaveChanges();
                }
                return RedirectToAction("Film", new { id = m.id });
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Film(int id)
        {
            var m = myOeuvre.getMoviesById(id);
            if (m == null)
                return View("NotFound");
            else
                return View("Films/ficheFilm", m);
        }

        public ActionResult EditFilm(int id)
        {
            var m = myOeuvre.getMoviesById(id);
            if (m == null)
                return View("NotFound");
            else
                return View("Films/editFilm", myOeuvre);
        }

        [HttpPost]
        public ActionResult EditFilm(int id, FormCollection collection)
        {
            var o = db.movies.First(i => i.id == id);
            try
            {
                if (o == null)
                    return View("NotFound");
                else
                {
                    o.title = collection["title"];
                    o.length = int.Parse(collection["length"]);
                    o.release_year = int.Parse(collection["release_year"]);
                    o.resume = collection["resume"];
                    db.movies.Attach(o);
                    db.movies.ApplyCurrentValues(o);
                    db.SaveChanges();
                }
                return RedirectToAction("Films");
            }
            catch
            {
                return RedirectToAction("EditFilm", new { id = o.id });
            }
        }

        public ActionResult DeleteFilm(int id)
        {
            try
            {
                var o = db.movies.First(i => i.id == id);
                db.movies.DeleteObject(o);
                db.SaveChanges();
                return RedirectToAction("Films");
            }
            catch
            {
                return RedirectToAction("Films");
            }
        }


        public ActionResult CategorizedFilm(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                categorized cat = new categorized
                {
                     movie = id,
                     category = int.Parse(data["categorie"])
                };

                if (TryUpdateModel(cat))
                {
                    db.categorized.AddObject(cat);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("EditFilm", new { id = id });
        }

        public ActionResult UncategorizedFilm(int id, FormCollection data)
        {
            if (data.Count > 0)
            {
                int category = int.Parse(data["categorie"]);
                var o = db.categorized.Where(c => c.movie == id)
                                      .Where(c => c.category == category)
                                      .First();
                db.categorized.DeleteObject(o);
                db.SaveChanges();
            }
            return RedirectToAction("EditFilm", new { id = id });
        }
        /*****************************************************/
        //
        // GET: /Admin/Oeuvres/Details/5

        public ActionResult Details(int id)
        {
            var o = db.oeuvres.First(i => i.id == id);
            if (o == null)
                return View("NotFound");
            else
                return View(o);
        }

        //
        // GET: /Admin/Oeuvres/Create

        public ActionResult Create()
        {
            Oeuvres o = new Oeuvres();
            ViewData.Add("authors",o.getAuthor());
            return View(ViewData);
        } 

        //
        // POST: /Admin/Oeuvres/Create

        [HttpPost]
        public ActionResult Create(string type, string title, int author)
        {
            try
            {
                oeuvre o = new oeuvre
                {
                    titre = title,
                    //  auteur = int(Request.Form["auteur"]),
                    type = type,
                    auteur = author
                };

                if (TryUpdateModel(o))
                {
                    db.oeuvres.AddObject(o);
                    db.SaveChanges();
                }
                return RedirectToAction("Details", new { id = o.id });
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Admin/Oeuvres/Edit/5
 
        public ActionResult Edit(int id)
        {
            var o = db.oeuvres.First(i => i.id == id);
            if (o == null)
                return View("NotFound");
            else
                return View(o);
        }

        //
        // POST: /Admin/Oeuvres/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var o = db.oeuvres.First(i => i.id == id);
            try
            {
                if (o == null)
                    return View("NotFound");
                else
                {
                    o.titre = Request.Form["titre"];
                    //  auteur = int(Request.Form["auteur"]),
                    o.type = "dvd";
                    db.SaveChanges();
                }
                return RedirectToAction("Details", new { id = o.id });
            }
            catch
            {
                return RedirectToAction("Details", new { id = o.id });
            }
        }

        //
        // GET: /Admin/Oeuvres/Delete/5
 
        public ActionResult Delete(int id)
        {
            var o = db.oeuvres.First(i => i.id == id);
            ViewData["URLReferrer"] = Request.UrlReferrer;
            return View(o);
        }

        //
        // POST: /Admin/Oeuvres/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var o = db.oeuvres.First(i => i.id == id);
                db.DeleteObject(o);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
